﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using System.Data;

namespace Portal.Web.Utility
{
    public class WebGeneratePageUtility
    {
        public static AjaxOut Draw(string SiteId, RenderInfoCls ORenderInfo)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string XmlDataPath = WebConfig.GetConfigFromXml(OSiteParam, "XmlDataPath");
                string PageAlias = ORenderInfo.Alias;
                string LayoutFile =XmlDataPath+"\\Pages\\"+ PageAlias + ".lay";
                string XmlFile = XmlDataPath + "\\Pages\\" + PageAlias + ".xml";
                DataSet ds=new DataSet();
                ds.ReadXml(XmlFile);
                string XmlData=ds.GetXml();
                PageCls OPage = PageParser.ParseFromXml(XmlData);
                string LayoutText=System.IO.File.ReadAllText(LayoutFile);
                for (int iIndexZone = 0; iIndexZone < OPage.PageZones.Count; iIndexZone++)
                {
                    string HtmlZoneText = "";
                    for (int iIndexPageZoneModule = 0; iIndexPageZoneModule < OPage.PageZones[iIndexZone].PageZoneModules.Count; iIndexPageZoneModule++)
                    {
                        string PlugInWebPartId=OPage.PageZones[iIndexZone].PageZoneModules[iIndexPageZoneModule].PlugInViewId;
                        string ServiceId=OPage.PageZones[iIndexZone].PageZoneModules[iIndexPageZoneModule].ServiceId;
                        IPluginView FoundPlugInView = PlugInViewUtility.FindOnePlugInView(SiteId, ServiceId, PlugInWebPartId);
                        if (FoundPlugInView != null)
                        {
                            ORenderInfo.PlugInViewServiceId = FoundPlugInView.ServiceId;
                            ORenderInfo.ModuleServiceId = FoundPlugInView.PlugInWebPartId;
                            ORenderInfo.DataId= OPage.PageZones[iIndexZone].PageZoneModules[iIndexPageZoneModule].ModuleId;
                            ORenderInfo.AppendClass = OPage.PageZones[iIndexZone].PageZoneModules[iIndexPageZoneModule].CssClass;
                            ORenderInfo.XmlDataConfig = OPage.PageZones[iIndexZone].PageZoneModules[iIndexPageZoneModule].XmlDataConfig;
                            AjaxOut OAjaxOutModuleRender = FoundPlugInView.Draw(SiteId, ORenderInfo);
                            HtmlZoneText = HtmlZoneText+OAjaxOutModuleRender.HtmlContent+"\r\n";
                        }
                    }
                    LayoutText=LayoutText.Replace("#" + OPage.PageZones[iIndexZone].ZoneAlias + "#", HtmlZoneText);
                }
                OAjaxOut.HtmlContent = LayoutText;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}
